package cn.bgs.wjx.WjxService.lmpl;

import cn.bgs.ActivityManagement.pojo.Announcement;
import cn.bgs.CommunityManagement.pojo.*;
import cn.bgs.DealershipManagement.pojo.VehicleCar;
import cn.bgs.DealershipManagement.pojo.VehicleMonthcar;
import cn.bgs.UserManagement.pojo.File;
import cn.bgs.UserManagement.pojo.User;
import cn.bgs.UserManagement.pojo.VisitorsRecord;
import cn.bgs.wjx.WjxMapper.WjxMapper;
import cn.bgs.wjx.WjxService.WjxService;
import cn.bgs.wjx.pojo.CellIntermediateWvo;
import cn.bgs.wjx.pojo.VehicleCarWvo;
import cn.bgs.wjx.pojo.VisitorsRecordWvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WjxServicelmpl implements WjxService {
    @Autowired
    WjxMapper wjxMapper;
    @Override
    public Announcement qqq(int i) {
        return wjxMapper.qqq(i);
    }

    @Override
    public void WjxAddVisitorsRecord(VisitorsRecord visitorsRecord) {
        wjxMapper.WjxAddVisitorsRecord(visitorsRecord);
    }

    @Override
    public User WjxLogin(User user) {
        return wjxMapper.WjxLogin(user);
    }

    @Override
    public List<CellIntermediateWvo> WjxFindDeaultState(Integer userId,Integer middleId) {
        return wjxMapper.WjxFindDeaultState(userId,middleId);
    }

    @Override
    public List<VisitorsRecordWvo> WjxRecorddetails(Integer userId, String startTime, String endTime) {
        return wjxMapper.WjxRecorddetails(userId,startTime,endTime);
    }

    @Override
    public void WjxinsertFile(File file2) {
        wjxMapper.WjxinsertFile(file2);
    }

    @Override
    public List<Community> WjxFindAllCommunity() {
        return wjxMapper.WjxFindAllCommunity();
    }

    @Override
    public List<Building> WjxFindAllBuilding() {
        return wjxMapper.WjxFindAllBuilding();
    }

    @Override
    public List<Floor> WjxFindAllFloor() {
        return wjxMapper.WjxFindAllFloor();
    }

    @Override
    public List<Portal> WjxFindAllPortal() {
        return wjxMapper.WjxFindAllPortal();
    }

    @Override
    public void cellIntermediate(CellIntermediateWvo cellIntermediate) {
        System.out.println("-------:"+cellIntermediate);
        if (cellIntermediate.getDefaultState().equals("是")){
            wjxMapper.cellIntermediate(cellIntermediate);
            //如果是 修改原来的默认房屋
            Integer id=wjxMapper.WjxFindDeaultState2(cellIntermediate);
            System.out.println("----:"+id);
            wjxMapper.DeletePortalstate(id);
            wjxMapper.updatePortalYes(cellIntermediate.getPortalId());
        }else {
            wjxMapper.cellIntermediate(cellIntermediate);
            wjxMapper.updatePortalNo(cellIntermediate.getPortalId());
        }

    }

    @Override
    public List<VehicleCarWvo> WjxFindVehicleCar(Integer userId) {
        return wjxMapper.WjxFindVehicleCar(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void WjxAddVehicleCar(VehicleCarWvo car) {
        wjxMapper.WjxAddVehicleCar(car);
        System.out.println("vehicleCarId：：："+car.getVehicleCarId());
        File file = new File();
        file.setFile(car.getFile());
        file.setAssociatedId(car.getVehicleCarId());
        file.setType("车辆");
        wjxMapper.wjxAddFile(file);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void WjxUpdateVehicleCar(VehicleCarWvo carWvo) {
        wjxMapper.WjxUpdateVehicleCar(carWvo);
        File file = new File();
        file.setFile(carWvo.getFile());
        file.setAssociatedId(carWvo.getVehicleCarId());
        file.setType("车辆");
        System.out.println("file:"+file);
        wjxMapper.wjxUpdateFile(file);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void WjxDeleteVehicleCar(VehicleCarWvo carWvo) {
        wjxMapper.WjxDeleteVehicleCar(carWvo);
        wjxMapper.wjxDeleteFile(carWvo.getVehicleCarId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void WjxAddMonthCar(VehicleCarWvo carWvo) {
        String serialNumber=String.valueOf((Math.random()*9+1)*100000);

        VehicleMonthcar vehicleMonthcar = new VehicleMonthcar();
        vehicleMonthcar.setSerialNumber(serialNumber);
        vehicleMonthcar.setVehicleCarId(carWvo.getVehicleCarId());
        vehicleMonthcar.setUserId(carWvo.getUserId());
        vehicleMonthcar.setEffectTime(carWvo.getCreateTime());
        vehicleMonthcar.setDueTime(carWvo.getUpdateTime());

        wjxMapper.WjxAddMonthCar(vehicleMonthcar);
        wjxMapper.WjxUpdateCar(carWvo.getVehicleCarId());
        wjxMapper.WjxDeleteUserMoney(carWvo.getUserId());
    }

    @Override
    public Object getUserImg(Integer userId) {
        return wjxMapper.getUserImg(userId);
    }

    @Override
    public void UpdateCellIntermediate(CellIntermediateWvo cellIntermediate) {
        if (cellIntermediate.getDefaultState().equals("是")){
            wjxMapper.UpdateCellIntermediate(cellIntermediate);
            //如果是 修改原来的默认房屋(拿到门户id)
            Integer id=wjxMapper.WjxFindDeaultState2(cellIntermediate);
            System.out.println("----:"+id);
            //修改原来的默认房屋 为不是
            wjxMapper.DeletePortalstate(id);
            //修改现在的门户 为是
            wjxMapper.updatePortalYes(cellIntermediate.getPortalId());
        }else {
            //修改中间表
            wjxMapper.UpdateCellIntermediate(cellIntermediate);
            //修改房屋默认状态为 不是
            wjxMapper.updatePortalNo(cellIntermediate.getPortalId());
        }
    }

    @Override
    public void deleteFangwu(Integer middleId) {
        wjxMapper.deleteFangwu(middleId);
    }

    @Override
    public List<VisitorsRecordWvo> findTCJF(Integer userId) {
        return wjxMapper.findTCJF(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void WjxStartJF(VisitorsRecordWvo visitorsRecordWvo) {
        wjxMapper.deleUserMoney(visitorsRecordWvo.getUserId(),visitorsRecordWvo.getMoney());
        wjxMapper.deleteTcjf(visitorsRecordWvo.getVisitorsRecordId());
    }

}
